import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/register_controller.dart';
import '../widgets/auth_header.dart';
import '../../shared/widgets/forms/app_text_field.dart';
import '../../shared/widgets/buttons/app_button.dart';

/// 注册视图
class RegisterView extends GetView<RegisterController> {
  const RegisterView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('创建账户'),
        elevation: 0,
        backgroundColor: Colors.transparent,
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
          child: Form(
            key: controller.formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // 头部
                const AuthHeader(
                  title: '开始创建您的账户',
                  subtitle: '填写以下信息以完成注册',
                ),
                
                const SizedBox(height: 30),
                
                // 姓名输入框
                AppTextField(
                  controller: controller.nameController,
                  labelText: '姓名/昵称',
                  hintText: '请输入您的姓名或昵称',
                  keyboardType: TextInputType.name,
                  prefixIcon: const Icon(Icons.person_outline),
                  validator: controller.validateName,
                ),
                
                const SizedBox(height: 20),
                
                // 邮箱输入框
                AppTextField(
                  controller: controller.emailController,
                  labelText: '邮箱',
                  hintText: '请输入您的邮箱地址',
                  keyboardType: TextInputType.emailAddress,
                  prefixIcon: const Icon(Icons.email_outlined),
                  validator: controller.validateEmail,
                ),
                
                const SizedBox(height: 20),
                
                // 密码输入框
                Obx(() => AppTextField(
                  controller: controller.passwordController,
                  labelText: '密码',
                  hintText: '请输入密码（至少6位）',
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: !controller.isPasswordVisible.value,
                  prefixIcon: const Icon(Icons.lock_outline),
                  suffixIcon: IconButton(
                    icon: Icon(
                      controller.isPasswordVisible.value
                          ? Icons.visibility_off_outlined
                          : Icons.visibility_outlined,
                    ),
                    onPressed: controller.togglePasswordVisibility,
                  ),
                  validator: controller.validatePassword,
                )),
                
                const SizedBox(height: 20),
                
                // 确认密码输入框
                Obx(() => AppTextField(
                  controller: controller.confirmPasswordController,
                  labelText: '确认密码',
                  hintText: '请再次输入密码',
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: !controller.isConfirmPasswordVisible.value,
                  prefixIcon: const Icon(Icons.lock_outline),
                  suffixIcon: IconButton(
                    icon: Icon(
                      controller.isConfirmPasswordVisible.value
                          ? Icons.visibility_off_outlined
                          : Icons.visibility_outlined,
                    ),
                    onPressed: controller.toggleConfirmPasswordVisibility,
                  ),
                  validator: controller.validateConfirmPassword,
                )),
                
                const SizedBox(height: 20),
                
                // 同意条款
                Obx(() => CheckboxListTile(
                  value: controller.agreeToTerms.value,
                  onChanged: controller.toggleAgreeToTerms,
                  title: RichText(
                    text: TextSpan(
                      text: '我已阅读并同意 ',
                      style: TextStyle(color: Get.theme.textTheme.bodyMedium?.color),
                      children: [
                        TextSpan(
                          text: '服务条款',
                          style: TextStyle(color: Get.theme.primaryColor, fontWeight: FontWeight.bold),
                          recognizer: TapGestureRecognizer()
                            ..onTap = controller.openTermsPage,
                        ),
                        const TextSpan(text: ' 和 '),
                        TextSpan(
                          text: '隐私政策',
                          style: TextStyle(color: Get.theme.primaryColor, fontWeight: FontWeight.bold),
                          recognizer: TapGestureRecognizer()
                            ..onTap = controller.openPrivacyPolicyPage,
                        ),
                      ],
                    ),
                  ),
                  controlAffinity: ListTileControlAffinity.leading,
                  contentPadding: EdgeInsets.zero,
                )),
                
                const SizedBox(height: 30),
                
                // 注册按钮
                Obx(() => AppButton(
                  text: '注册',
                  onPressed: controller.register,
                  isLoading: controller.isLoading.value,
                )),
                
                const SizedBox(height: 20),
                
                // 登录链接
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text('已经有账户了?'),
                    TextButton(
                      onPressed: controller.navigateToLogin,
                      child: const Text('立即登录'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
} 